package com.tozed.android.launcher.simpleui;

import com.tozed.android.launcher.R;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.Button;

public class AddApplication extends Activity implements OnClickListener
{
	private static final String TAG = "AddApplication";
	
	private static final String ADD_APPLICATION_CUREENTTAB = "SimpleFolder_customize_currentTab";
	private static final String ALL_APP_TAG_FLAG = "all apps";
	private static final String ALL_DOWN_LOAD_TAG_FLAG = "download apps";
	
	private ActionBar mActionBar;
	private AddAppViewPager mAddAppViewPager;
	private Button mBtnAllApps;
	private Button mBtnDownloadApps;
	private String mCurrentType;
	  
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		mActionBar = getActionBar();
	    View allAppsView = AddApplicationViewCache.getInstance().getAllAppsView();
	    
	    int useCacheLayout = 0;
	    
	    if (allAppsView == null){
	      Log.i(TAG, "onCreate use inflate layout");
	      allAppsView = getLayoutInflater().inflate(R.layout.simple_add_app, null);
	      AddApplicationViewCache.getInstance().setAllAppsView(allAppsView);
	    }else{
	    	Log.i(TAG, "onCreate use cached layout");
	    	useCacheLayout = 1;
	    }

		ViewParent viewParent = allAppsView.getParent();
		if (viewParent != null && viewParent instanceof ViewGroup)
			((ViewGroup)viewParent).removeView(allAppsView);

		allAppsView.setVisibility(View.VISIBLE);

		setContentView(allAppsView);

		mActionBar.setDisplayHomeAsUpEnabled(true);
		mAddAppViewPager = ((AddAppViewPager) allAppsView.findViewById(R.id.view_pager));

		if (useCacheLayout != 0){
			mAddAppViewPager.updateChildContext(this);
			mAddAppViewPager.updateRecentView();
			mAddAppViewPager.clearSearchText();
		}

		mBtnAllApps = ((Button) findViewById(R.id.btn_all_apps));
		mBtnDownloadApps = ((Button) findViewById(R.id.btn_downloaded_apps));
		setupClickListener();
		mCurrentType = ALL_APP_TAG_FLAG;
		onTabChanged(mCurrentType);
		
		restoreState(savedInstanceState);
	}

	@Override
	public void onClick(View v)
	{
		Object tag = v.getTag();
		
		if (tag instanceof SimpleApplicationInfo){
			SimpleApplicationInfo app = (SimpleApplicationInfo)tag;
			
			Intent intent = new Intent();
			intent.putExtra("android.intent.extra.shortcut.INTENT", app.intent);
			setResult(RESULT_OK, intent);
			finish();
		}
	}
	
	@Override
	protected void onStop()
	{
		super.onStop();
		finish();
	}
	
	@Override
	public void onBackPressed()
	{
		super.onBackPressed();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState)
	{
		outState.putString(ADD_APPLICATION_CUREENTTAB, mCurrentType);
		super.onSaveInstanceState(outState);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
	    super.onBackPressed();
	    
	    setResult(0);
	    finish();
	    
	    return true;
	}
	
	private void onTabChanged(String paramString){
		mAddAppViewPager.changePage(paramString);
	}

	private void restoreState(Bundle state)
	{
		if (state == null){
			setCurrentType(ALL_APP_TAG_FLAG);
		}else{
			mCurrentType = state.getString(ADD_APPLICATION_CUREENTTAB, null);
			
			if (mCurrentType == null){
				setCurrentType(ALL_APP_TAG_FLAG);
			}else{
				setCurrentType(mCurrentType);
			}
		}
	}
	
	public void setCurrentType(String strType){
		mCurrentType = strType;
		updateSelectedView();
	}
	
	private void updateSelectedView()
	{
		if (mCurrentType != null && mCurrentType.equals(ALL_APP_TAG_FLAG))
		{
			mBtnAllApps.setBackgroundResource(R.drawable.sub_tab_selected_left);
			mBtnAllApps.setTextColor(Color.WHITE);
			mBtnDownloadApps.setBackgroundResource(R.drawable.subtab_style_right);
			mBtnDownloadApps.setTextColor(Color.BLACK);
		}else{
			mBtnAllApps.setBackgroundResource(R.drawable.subtab_style_left);
			mBtnAllApps.setTextColor(Color.BLACK);
			mBtnDownloadApps.setBackgroundResource(R.drawable.sub_tab_selected_right);
			mBtnDownloadApps.setTextColor(-1);
		}
	}
	
	private void setupClickListener()
	{
		mBtnAllApps.setOnClickListener(new View.OnClickListener(){
			public void onClick(View view){
				mCurrentType = ALL_APP_TAG_FLAG;
				updateSelectedView();
				onTabChanged(mCurrentType);
			}
		});
		
		mBtnDownloadApps.setOnClickListener(new View.OnClickListener(){
			public void onClick(View view){
				mCurrentType = ALL_DOWN_LOAD_TAG_FLAG;
				updateSelectedView();
				onTabChanged(mCurrentType);
			}
		});
	}
}
